package com.zens.cms.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.zens.cms.entity.TColumn;
import com.zens.cms.entity.TColumnExample;
import com.zens.cms.entity.TColumnExample.Criteria;
import com.zens.cms.mapper.TColumnMapper;
import com.zens.cms.request.ToolHelpRequest;
import com.zens.cms.services.ITColumn;

@Service
public class TColumnService implements ITColumn{
	
	@Resource
	TColumnMapper mapper;
	
	/* 
	 * add column
	 */
	public Long add(TColumn col){
		mapper.insertSelective(col);
		return col.getId();
	
		
	}
	/**
	 * update column
	 */
	public int update(TColumn col) {
		return mapper.updateByPrimaryKeySelective(col);
	}
	/**
	 * delete column by id
	 */
	public int delete(Long id) {
		return mapper.deleteByPrimaryKey(id);
	}
	/**
	 * query column by id
	 */
	public TColumn query(Long id) {
		return mapper.selectByPrimaryKey(id);
	}
	/**
	 * query column by name
	 */
	public List<TColumn> queryByname(TColumn col) {
		TColumnExample example = new TColumnExample();
		Criteria  criteria = example.createCriteria();
		criteria.andFcolumnnameEqualTo(col.getFcolumnname());
		return mapper.selectByExample(example);
	}
	public List<TColumn> query(TColumn col) {
		col.getHolder().value = mapper.countQueryList(col);
		return mapper.queryTree(col);
	}
	public List<TColumn> queryBySiteID(TColumn col) {
		TColumnExample example = new TColumnExample();
		example.setOrderByClause("FSortNum*1");
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(col.getFsiteid());
		criteria.andFcheckedEqualTo("1");
		return mapper.selectByExample(example);
	}
	
	public List<TColumn> queryBySiteIDandLevel(TColumn col) {
		TColumnExample example = new TColumnExample();
		example.setOrderByClause("FSortNum*1");
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(col.getFsiteid());
		criteria.andFcolumnlevelLessThanOrEqualTo(col.getFcolumnlevel());
		return mapper.selectByExample(example);
	}
	public List<TColumn> queryByColumnGP(Long id) {
		TColumnExample example = new TColumnExample();
		Criteria  criteria = example.createCriteria();
		criteria.andFcolumngpEqualTo(id);
		return mapper.selectByExample(example);
	}
	public List<TColumn> querybyFsortNum(TColumn col,String FSortNum) {
		TColumnExample example = new TColumnExample();
		example.setOrderByClause("FSortNum*1");
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(col.getFsiteid());
		//criteria.andFsortnumGreaterThanOrEqualTo(col.getFsortnum());
		criteria.andFsortnumBetween(Integer.parseInt(col.getFsortnum()), Integer.parseInt(FSortNum));
		return mapper.selectByExample(example);
	}
	public List<TColumn> querybyFColumnLevel(TColumn col) {
		TColumnExample example = new TColumnExample();
		example.setOrderByClause("FSortNum*1");
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(col.getFsiteid());
		criteria.andFcheckedEqualTo("1");
		criteria.andFsortnumGreaterThanOrEqualTo(Integer.parseInt(col.getFsortnum()));
		criteria.andFcolumnlevelLessThanOrEqualTo(col.getFcolumnlevel());
		return mapper.selectByExample(example);
	}
	public List<TColumn> queryColumnByASC(Long id) {
		TColumn tc = query(id);
		TColumnExample example = new TColumnExample();
		example.setOrderByClause("FSortNum*1");
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(tc.getFsiteid());
		criteria.andFcheckedEqualTo("1");
		
		criteria.andFsortnumGreaterThanOrEqualTo(Integer.parseInt(tc.getFsortnum()));
		
		criteria.andFcolumnlevelGreaterThanOrEqualTo(tc.getFcolumnlevel());
		return mapper.selectByExample(example);
	}
	public List<TColumn> queryColumnByDESC(Long id) {
		TColumn tc = query(id);
		TColumnExample example = new TColumnExample();
		example.setOrderByClause("FSortNum*1");
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(tc.getFsiteid());
		criteria.andFcheckedEqualTo("1");
		
		criteria.andFsortnumLessThanOrEqualTo(Integer.parseInt(tc.getFsortnum()));
		
		criteria.andFcolumnlevelLessThanOrEqualTo(tc.getFcolumnlevel());
		return mapper.selectByExample(example);
	}
	public List<TColumn> querybyFColumnLevelToLastOne(TColumn col) {
		TColumnExample example = new TColumnExample();
		example.setOrderByClause("FSortNum*1");
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(col.getFsiteid());
		criteria.andFsortnumGreaterThanOrEqualTo(Integer.parseInt(col.getFsortnum()));
		return mapper.selectByExample(example);
	}
	/**
	 * 
	 */
	public List<TColumn> queryPreColumn(Long id) {
		TColumnExample example = new TColumnExample();
		Criteria  criteria = example.createCriteria();
		criteria.andIdEqualTo(mapper.selectByPrimaryKey(id).getFcolumngp().longValue());
		criteria.andFcheckedEqualTo("1");
		return mapper.selectByExample(example);
	}
	public int count(TColumn col){
		
		TColumnExample example = new TColumnExample();
		Criteria  criteria = example.createCriteria();
		criteria.andFsiteidEqualTo(col.getFsiteid());
		return mapper.countByExample(example);
	}
	public List<TColumn> columnSortNum(TColumn col)
	{
		return mapper.querysortNum(col);
	}
	public int deleteByExample(List<Long> list)
	{
		TColumnExample example = new TColumnExample();
		com.zens.cms.entity.TColumnExample.Criteria criteria = example.createCriteria();
	    criteria.andIdIn(list);
	    int res = mapper.deleteByExample(example);
	    return res;
	}
	public int deleteBySiteID(String FSiteID)
	{
		TColumnExample example = new TColumnExample();
		com.zens.cms.entity.TColumnExample.Criteria criteria = example.createCriteria();
		
	    criteria.andFsiteidEqualTo(Long.valueOf(FSiteID));
	    int res = mapper.deleteByExample(example);
	    return res;
	}
	public  int formatTree(TColumn col) {
		 mapper.formatTree(col);
		 return 1;
	}
	public void importColumnSource(String sql) {
		ToolHelpRequest thr = new  ToolHelpRequest();
		thr.setSqlStr(sql);
		mapper.importColumnSource(thr);
		
	}
	public List<TColumn> getColumn(Long id,String require,int level)
	{
		List<TColumn> ltc = new ArrayList();
		if(require.matches(">="))
		{
			
			TColumn tc = query(id);
			
			List<TColumn> list = queryColumnByASC(id);
			List<TColumn> ltc_ = new ArrayList();
			for(int i = 0;i<list.size();i++)
			{
				if(list.get(i).getFcolumnlevel().toString().matches(tc.getFcolumnlevel().toString())&&!list.get(i).getId().toString().matches(tc.getId().toString()))
				{
					break;
				}
				ltc_.add(list.get(i));
				
			}
			for(int j = 0;j<ltc_.size();j++)
			{
				if(list.get(j).getFcolumnlevel()<=tc.getFcolumnlevel()+level)
				{
					ltc.add(list.get(j));
				}
			}
			
		}
		else if(require.matches(">"))
		{
			
			TColumn tc = query(id);
			
			List<TColumn> list = queryColumnByASC(id);
			List<TColumn> ltc_ = new ArrayList();
			for(int i = 0;i<list.size();i++)
			{
				if(list.get(i).getFcolumnlevel().toString().matches(tc.getFcolumnlevel().toString())&&!list.get(i).getId().toString().matches(tc.getId().toString()))
				{
					break;
				}
				ltc_.add(list.get(i));
			}
			for(int j = 0;j<ltc_.size();j++)
			{
				if(list.get(j).getFcolumnlevel()<=tc.getFcolumnlevel()+level&&j!=0)
				{
					ltc.add(list.get(j));
				}
			}
			
		}
		else if(require.matches("<="))
		{
			
			TColumn tc = query(id);
			
			List<TColumn> list = queryColumnByDESC(id);
			List<TColumn> ltc_ = new ArrayList();
			for(int i = list.size()-1;i>=0;i--)
			{
				if(list.get(i).getFcolumnlevel()<tc.getFcolumnlevel()-level)
				{
					break;
				}
				
				
					ltc_.add(list.get(i));
				
			}
			int ind = ltc_.get(0).getFcolumnlevel();
			for(int j = 0;j<ltc_.size();j++)
			{
				if(ltc_.get(j).getFcolumnlevel()==ind)
				{
					
						ind--;
						ltc.add(ltc_.get(j));
					
				}
			}
			
		}
		else if(require.matches("<"))
		{
			
			TColumn tc = query(id);
			
			List<TColumn> list = queryColumnByDESC(id);
			List<TColumn> ltc_ = new ArrayList();
			for(int i = list.size()-1;i>=0;i--)
			{
				if(list.get(i).getFcolumnlevel()<tc.getFcolumnlevel()-level)
				{
					break;
				}
				ltc_.add(list.get(i));
			}
			int ind = ltc_.get(0).getFcolumnlevel();
			for(int j = 0;j<ltc_.size();j++)
			{
				if(ltc_.get(j).getFcolumnlevel()==ind)
				{
					
						ind--;
						if(j!=0)
						{
							ltc.add(ltc_.get(j));
						}
				}
			}
			
		}
		return ltc;
	}
}
